﻿using MD5Hash;
using MediatR;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.Domain.RbacEntity;
using RBAC.Infrasturcture.Interfaces;
using RBAC.ResultCode;

namespace RBAC.ApiWrites.Applocation.Commandhandler
{
    public class AddUserfunctionsCommandhandler : IRequestHandler<AddUserfunctionsCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<User> userrepository;
        private readonly IBaseRepository<UserRole> userrolerepository;
        private readonly ILogger<AddUserfunctionsCommandhandler> logger;
        private readonly IBaseRepository<UserFunction> userFunctionrepository;

        public AddUserfunctionsCommandhandler(IBaseRepository<User> userrepository,IBaseRepository<UserRole> userrolerepository,ILogger<AddUserfunctionsCommandhandler> logger,IBaseRepository<Function> Functionrepository,IBaseRepository<UserFunction> userFunctionrepository) 
        {
            this.userrepository = userrepository;
            this.userrolerepository = userrolerepository;
            this.logger = logger;
            this.userFunctionrepository = userFunctionrepository;
        }
        public async Task<ApiResult<int>> Handle(AddUserfunctionsCommand request, CancellationToken cancellationToken)
        {
            using (var tran= userrepository.DB.Database.BeginTransaction())
            {
                try
                {  User  user=new User();
                     user.UserName = request.UserName;
                    user.UserPwd=request.UserPwd.GetMD5();
                     user.CreateTime = DateTime.Now;
                    user.Status = 1;
                var res=    await userrepository.AddAsync(user);



                    List<UserRole> userRoles = new List<UserRole>();

                    foreach (var item in request.RoleId)
                    {
                        userRoles.Add(new UserRole() {
                             UserId=user.UserId,
                            
                            RoleId = item }); 


                    }
                    await userrolerepository.AddRange(userRoles);

                    List<UserFunction> userfunction = new List<UserFunction>();

                    foreach (var item in request.Functions)
                    {
                        userfunction.Add(new UserFunction()
                        {
                            UserId = user.UserId,

                             FunctionId  = item
                        });


                    }
                    await userFunctionrepository.AddRange(userfunction);


                    tran.Commit();

                    return new ApiResult<int>
                    {

                        Code = ApiResultEnum.新增成功,
                        Message = ApiResultEnum.新增成功.ToString(),
                        Data = res
                    };


                }
                catch (Exception ex)
                {
                    logger.LogError("=>" + ex.Message);

                    tran.Rollback();    
                    return new ApiResult<int>
                    {

                        Code = ApiResultEnum.新增失败,
                        Message = ApiResultEnum.新增失败.ToString(),
                        Data = -1
                    };

                }
                
            }
        }
    }
}
